package com.joker.aigc.tools.service.video.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joker.aigc.tools.domian.dto.CategoryAddDTO;
import com.joker.aigc.tools.domian.dto.CategoryDTO;
import com.joker.aigc.tools.domian.po.ActorsPO;
import com.joker.aigc.tools.domian.po.CategoriesPO;
import com.joker.aigc.tools.domian.po.SubCategoriesPO;
import com.joker.aigc.tools.mapper.ActorsMapper;
import com.joker.aigc.tools.mapper.CategoriesMapper;
import com.joker.aigc.tools.mapper.SubCategoriesMapper;
import com.joker.aigc.tools.service.video.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分类服务实现类
 *
 * @author system
 * @date 2023-12-01
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoriesMapper categoriesMapper;

    @Resource
    private SubCategoriesMapper subCategoriesMapper;


    @Resource
    private ActorsMapper actorsMapper;

    @Override
    public List<CategoryDTO> getAllCategories() {
        try {
            return categoriesMapper.getAllCategoriesWithSub();
        } catch (Exception e) {
            log.error("获取所有分类失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取所有分类失败: " + e.getMessage());
        }
    }

    @Override
    public List<CategoryDTO> getCategoriesByType(String categoryType) {
        try {
            Long type = convertCategoryType(categoryType);
            return categoriesMapper.getCategoriesByType(type);
        } catch (Exception e) {
            log.error("根据类型获取分类失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据类型获取分类失败: " + e.getMessage());
        }
    }

    @Override
    public List<CategoryDTO.SubCategoryDTO> getSubCategories(Long categoryId) {
        try {
            return subCategoriesMapper.getSubCategoriesByCategoryId(categoryId);
        } catch (Exception e) {
            log.error("获取子分类失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取子分类失败: " + e.getMessage());
        }
    }

    /**
     * 获取演员
     *
     * @return 演员列表，如果为空，则返回所有演员
     */
    @Override
    public List<ActorsPO> getActors(String actorName) {
        LambdaQueryWrapper<ActorsPO> queryWrapper = new LambdaQueryWrapper<ActorsPO>();
        if (StringUtils.isNotBlank(actorName)) {
            queryWrapper.like(ActorsPO::getName, actorName);
        }
        return actorsMapper.selectList(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addCategory(CategoryAddDTO categoryAddDTO) {
        // 如果分类名称不存在，则创建一级分类。
        String name = categoryAddDTO.getName();
        String description = categoryAddDTO.getDescription();
        String subCategoryName = categoryAddDTO.getSubCategoryName();
        String subCategoryDescription = categoryAddDTO.getSubCategoryDescription();
        CategoriesPO data = categoriesMapper.selectOne(new LambdaQueryWrapper<>(CategoriesPO.class).eq(CategoriesPO::getName, name));
        if (data == null) {
            data = new CategoriesPO();
            data.setName(name);
            data.setDescription(description);
            data.setCategoryType(1L);
            // 创建一级分类
            categoriesMapper.insert(data);
        }
        SubCategoriesPO sub = new SubCategoriesPO();
        sub.setName(subCategoryName);
        sub.setDescription(subCategoryDescription);
        sub.setCategoryId(data.getCategoryId());
        subCategoriesMapper.insert(sub);
        return "添加成功";
    }

    /**
     * 转换分类类型字符串为数字
     *
     * @param categoryType 分类类型字符串
     * @return 分类类型数字
     */
    private Long convertCategoryType(String categoryType) {
        switch (categoryType.toLowerCase()) {
            case "genre":
                return 1L; // 类型分类
            case "region":
                return 2L; // 地域分类
            case "actor":
                return 3L; // 演员分类
            default:
                throw new IllegalArgumentException("不支持的分类类型: " + categoryType);
        }
    }
} 